package com.yang.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.yang.pojo.User;
import com.yang.service.UserService;
import com.yang.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
@RequestMapping("/sys/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 按条件查询用户并显示
     * @param model
     * @param user
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/toList",method = RequestMethod.GET)
    public String toList(Model model, User user, Integer pageNum, Integer pageSize){
        if (pageNum==null){
            pageNum=1;
        }
        if (pageSize==null){
            pageSize=5;
        }
        PageInfo<User> pageInfo = userService.queryByNameAndRole(user, pageNum, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("user",user);
        return "user_list";
    }

    /**
     * 前往添加页面
     * @return
     */
    @RequestMapping("/toSave")
    public String toaddUser(){
        return "user_save";
    }
    @ResponseBody
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String addUser(HttpSession session,User user){
        Object attribute = session.getAttribute(Common.LOGIN_USER);
        if (attribute==null){
            return "error2";
        }
        Integer integer = userService.addUser(user);
        if (integer>0){
            return "success";
        }
        return "error";
    }

    /**
     * 查看用户基本信息
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryById",method = RequestMethod.GET,
            produces = "application/json;charset=utf-8")
    public String queryById(Integer id){
        User user = userService.queryById(id);
        return JSON.toJSONString(user);
    }

    /**
     * 修改用户信息
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/toUpdate",method = RequestMethod.GET)
    public String toUpdate(Integer id,Model model){
        User user = userService.queryById(id);
        model.addAttribute("user",user);
        return "user_update";
    }
    @RequestMapping(value = "update",method = RequestMethod.GET)
    @ResponseBody
    public String updateUser(User user,HttpSession session){
        Object attribute = session.getAttribute(Common.LOGIN_USER);
        if (attribute==null){
            return "error2";
        }
        User loginUser=(User)attribute;
        user.setModifyBy(loginUser.getId());
        Integer integer = userService.updateUser(user);
        if (integer>0){
            return "success";
        }
        return "error";
    }

    /**
     * 删除用户信息
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public String delete(Integer userId){
        Integer count = userService.deleteUser(userId);
        if (count > 0) {
            return "success";
        }
        return "error";
    }
    @ResponseBody
    @RequestMapping(value = "/updateStatus",method = RequestMethod.GET)
    public String updateStatus(Integer id){
        Integer count = userService.updateStatus(id);
        if (count > 0) {
            return "success";
        }
        return "error";
    }
    /**
     * 前往当前用户密码修改页面
     */
    @RequestMapping("/toUpdatePassword")
    public String toupdatePassword(){
        return "update_password";
    }

    /**
     * 验证密码
     * @param oldPassword
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/checkPassword",method = RequestMethod.GET)
    public String checkPassword(String oldPassword,HttpSession session){
        Object attribute = session.getAttribute(Common.LOGIN_USER);
        if (attribute==null){
            return "error2";
        }
        User user = (User) attribute;
        if (oldPassword.equals(user.getUserPassword())){
            return "success";
        }
        return "error";
    }

    /**
     * 修改密码
     * @param userPassword
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updatePassword",method = RequestMethod.POST)
    public String updatePassword(String userPassword,HttpSession session){
        //获取session中的登录用户
        User loginUser = (User)session.getAttribute(Common.LOGIN_USER);
        User user = new User();
        user.setUserPassword(userPassword);
        user.setId(loginUser.getId());
        Integer count = userService.updatePassword(user);
        if (count>0){
            //清除session中的信息
            session.removeAttribute(Common.LOGIN_USER);
            return "success";
        }
        return "error";
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    //退出功能
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public String logout(HttpSession session){
        session.removeAttribute(Common.LOGIN_USER);
        return "login";
    }
}
